# -*- coding: utf-8 -*-
# created on 2016/12/22

from sympy import sympify
from mathsolver.functions.base import BaseFunction, BaseEq, BasePoint, new_latex
from mathsolver.functions.hanshu.helper import check_func
from mathsolver.functions.hanshu.zhouqi import has_f, is_faxplusb_anyb
from mathsolver.functions.sympy_utils import get_all_child, default_symbol
from mathsolver.functions.hanshu.dingyiyu import QiuDingYiYu


def duichengzhongxing_2funcs(expr1, expr2):
    """hs076.求两个函数对称中心
    f(a1*x+b)+m 和 -f(-a1*x+c)+n => ((c-b)/2*a1, (m+n)/2)
    """
    expr = expr1 + expr2
    var = default_symbol(expr)
    mplusn, fpart = expr.as_independent(var)

    fxs = get_all_child(fpart, has_f)
    ((a1, b), (a2, c)) = [is_faxplusb_anyb(fx) for fx in fxs]
    assert a1 == -a2
    if a1 > 0:
        res = ((c - b) / (2 * a1), mplusn / 2)
    else:
        res = ((b - c) / (2 * a2), mplusn / 2)
    step = "%s 和 %s 关于 %s 对称" % (expr1, expr2, res)
    return res, step


def duicheng_2funs_qiujiexishi(expr, point):
    """hs077.两个函数中心对称求函数解析式"""
    a, b = point
    jiexishi = sympify('-f(2*{a}-x)+2*{b}'.format(a=a, b=b))
    var = default_symbol(jiexishi)
    res = -expr.subs(var, 2 * a - var) + 2 * b
    step = "%s 关于 (%s, %s) 对称的图像解析式为 %s = %s" % (expr, a, b, jiexishi, res)
    return res, step


def duichengzhou_2funcs(expr1, expr2):
    """hs078.求两个函数对称轴
    f(a*x+b) 和 f(-a*x+c) 关于 x = (c-b)/(2*a) 对称
    """
    a1, b = is_faxplusb_anyb(expr1)
    a2, c = is_faxplusb_anyb(expr2)
    assert a1 == -a2
    if a1 > 0:
        res = (c - b) / (2 * a1)
    else:
        res = (b - c) / (2 * a2)
    step = "%s 和 %s 关于直线 x = %s 对称" % (expr1, expr2, res)
    return res, step


def zhouduicheng_2funs_qiujiexishi(expr, a):
    """hs079.两个函数轴对称求函数解析式"""
    jiexishi = sympify('f(2*{a}-x)'.format(a=a))
    var = default_symbol(expr)
    res = expr.subs(var, 2 * a - var)
    step = "%s 关于直线 x = %s 对称的图像的解析式为 %s = %s" % (expr, a, jiexishi, res)
    return res, step


class ZhongXingDuiChengQiuJieXiShi(BaseFunction):
    """函数中心对称求解析式"""

    def solver(self, *args):
        func = check_func(args[0])
        point = sympify(args[1].value) if args[1] else BasePoint([0, 0]).sympify()
        expr = func.expression

        res, step = duicheng_2funs_qiujiexishi(expr, point)
        self.steps.append(["", step])

        # TODO
        output_func = BaseEq(['f(x)', res]).eq2func()
        output_func.dingyiyu = QiuDingYiYu().solver(output_func)
        self.steps.append(["", "定义域为 %s 属于 %s" % (new_latex(func.var), new_latex(output_func.dingyiyu))])
        self.output.append(output_func)
        self.label.add("函数中心对称求解析式")
        return self


class ZhouDuiChengQiuJieXiShi(BaseFunction):
    """函数轴对称求函数解析式"""

    def solver(self, *args):
        func = check_func(args[0])
        expr = func.expression
        a = sympify(args[1].value[1])
        res, step = zhouduicheng_2funs_qiujiexishi(expr, a)

        self.steps.append(["", step])
        self.label.add("函数轴对称求函数解析式")
        self.output.append(BaseEq(['f', res]))
        return self


if __name__ == '__main__':
    pass
